package org.darkforest.admin.controller;

import org.darkforest.common.services.StoreGradeService;
import org.darkforest.model.StoreGrade;
import org.darkforest.ucenter.auth.AuthPassport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 店铺等级管理
 * Created by admin on 2015/9/6.
 */
@Controller
@RequestMapping("store")
public class StoreGradeController {


    @Autowired
    private StoreGradeService storeGradeService;




    /**
     * 店铺等级列表
     * @return
     */
    @AuthPassport
    @RequestMapping("levelList")
    public ModelAndView storeLevelList(
                                       @RequestParam(required = false, defaultValue = "") String levelName){


        StoreGrade storeGrade = new StoreGrade();
        if(!"".equals(levelName)){
            storeGrade.setSgName(levelName);
        }
        List<StoreGrade> storeGrades = storeGradeService.selectStoreGradeByCondition(storeGrade);

        ModelAndView modelAndView = new ModelAndView("store/level/levelList");
        modelAndView.addObject("storeGradeList",storeGrades);
        modelAndView.addObject("searchKey", levelName);
        return modelAndView;
    }

    /**
     * 跳转到添加店铺等级页面
     * @param request
     * @return
     */
    @RequestMapping(value = "levelList/toAddStoreGrade")
    public String toAddStoreGrade(HttpServletRequest request){
        return "store/level/add_store_grade";
    }

    /**
     * 执行添加店铺等级
     * @param storeGrade
     * @return
     */
    @RequestMapping(value = "doAddStoreGrade")
    public ModelAndView doAddStoreGrade(@ModelAttribute("storeGrade") StoreGrade storeGrade){

        storeGradeService.insertSelective(storeGrade);
        ModelAndView res = new ModelAndView("msg");
        res.addObject("msg","店铺等级添加成功");
        return res;
    }


    /**
     * 添加店铺等级校验等级名称以及等级级别
     * @param sg_sort 级别，数目越大级别越高
     * @param sg_name
     * @return
     */
    @RequestMapping(value = "checkStoreGradeNameOrSort")
    public
    @ResponseBody
    boolean checkStoreGradeNameOrSort(@RequestParam(required = false, defaultValue = "") String sg_sort,
                                      @RequestParam(required = false, defaultValue = "") String sg_name,
                                      @RequestParam(required = false, defaultValue = "") String sg_id
    ) {
        boolean result ;
        StoreGrade storeGrade = new StoreGrade();

        if (!"".equals(sg_sort)) {
            storeGrade.setSgSort(Byte.parseByte(sg_sort));
        }
        if (!"".equals(sg_name)) {
            storeGrade.setSgNameEq(sg_name);
        }
        List<StoreGrade> storeGradeList = storeGradeService.selectStoreGradeByCondition(storeGrade);
        if (storeGradeList.size() > 0) {
            result = false;
        } else {
            result = true;
        }
        return result;
    }



    /**
     * 编辑店铺等级校验等级名称以及等级级别
     * @param sg_sort 级别，数目越大级别越高
     * @param sg_name
     * @return
     */
    @RequestMapping(value = "checkStoreGradeNameOrSortForModify")
    public
    @ResponseBody
    boolean checkStoreGradeNameOrSortForModify(@RequestParam(required = false, defaultValue = "") String sg_sort,
                                      @RequestParam(required = false, defaultValue = "") String sg_name,
                                      @RequestParam(required = false, defaultValue = "") String sg_id
    ) {
        boolean result ;
        StoreGrade storeGrade = new StoreGrade();

        if (!"".equals(sg_sort)) {
            storeGrade.setSgSort(Byte.parseByte(sg_sort));
        }
        if (!"".equals(sg_name)) {
            storeGrade.setSgNameEq(sg_name);
        }
        if (!"".equals(sg_id)) {
            storeGrade.setSgId(Integer.parseInt(sg_id));
        }
        List<StoreGrade> storeGradeList = storeGradeService.checkStoreGradeNameOrSortForModify(storeGrade);
        if (storeGradeList.size() > 0) {
            result = false;
        } else {
            result = true;
        }
        return result;
    }




    /**
     * 批量删除设定的店铺等级
     * @param ids
     * @return
     */
    @RequestMapping(value = "delStoreGrade")
    public String delStoreGrade(String[] ids){
        int i = storeGradeService.delStoreGradeBatch(ids);

        return  "redirect: levelList";
    }

    /**
     * 跳转到店铺等级编辑页面
     * @param sgId
     * @return
     */
    @RequestMapping(value = "levelList/toModifyStoreGrade")
    public ModelAndView toModifyStoreGrade(Integer sgId){

        ModelAndView res = new ModelAndView("store/level/modify_store_grade");
        res.addObject("sg",storeGradeService.selectByPrimaryKey(sgId));
        return res;
    }


    /**
     * 修改设定的店铺等级
     * @param storeGrade
     * @return
     */
    @RequestMapping(value = "doModifyStoreGrade")
    public ModelAndView doModifyStoreGrade(@ModelAttribute("storeGrade") StoreGrade storeGrade){
        storeGradeService.updateByPrimaryKeySelective(storeGrade);
        ModelAndView res = new ModelAndView("msg");
        res.addObject("msg","店铺等级修改成功");
        return  res;
    }


}